<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="login-background">
      <div class="bg-gradient"></div>
      <div class="bg-pattern"></div>
    </div>

    <!-- 主要内容区域 -->
    <div class="login-content">
      <!-- 左侧信息面板 -->
      <div class="left-panel">
        <div class="brand-section">
          <div class="logo-container">
            <img src="@/assets/logo/logo-data-analysis-modern.svg" alt="系统Logo" class="system-logo">
          </div>
          <h1 class="system-title">xx数据分析系统</h1>
          <p class="system-subtitle">XX Data Analysis System</p>
          
          <div class="system-features">
            <div class="feature-item">
              <div class="feature-icon">
                <svg-icon icon-class="chart" />
              </div>
              <div class="feature-text">
                <h3>智能数据分析</h3>
                <p>深度挖掘数据价值</p>
              </div>
            </div>
            
            <div class="feature-item">
              <div class="feature-icon">
                <svg-icon icon-class="dashboard" />
              </div>
              <div class="feature-text">
                <h3>实时监控面板</h3>
                <p>掌握业务运行状态</p>
              </div>
            </div>
            
            <div class="feature-item">
              <div class="feature-icon">
                <svg-icon icon-class="excel" />
              </div>
              <div class="feature-text">
                <h3>多维度报表</h3>
                <p>助力智能决策</p>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧登录表单 -->
      <div class="right-panel">
        <div class="login-form-container">
          <div class="login-header">
            <h2>系统登录</h2>
            <p>欢迎使用xx数据分析系统</p>
          </div>

          <el-form 
            ref="loginForm" 
            :model="loginForm" 
            :rules="loginRules" 
            class="login-form"
            size="large"
          >
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                type="text"
                auto-complete="off"
                placeholder="请输入用户名"
                prefix-icon="el-icon-user"
              />
            </el-form-item>

            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                auto-complete="off"
                placeholder="请输入密码"
                prefix-icon="el-icon-lock"
                @keyup.enter.native="handleLogin"
              />
            </el-form-item>

            <el-form-item prop="code" v-if="captchaEnabled" class="code-item">
              <el-input
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="请输入验证码"
                prefix-icon="el-icon-picture-outline"
                style="width: 60%"
                @keyup.enter.native="handleLogin"
              />
              <div class="captcha-container">
                <img :src="codeUrl" @click="getCode" class="captcha-img" alt="验证码"/>
              </div>
            </el-form-item>

            <el-form-item>
              <div class="login-options">
                <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
                <span class="forgot-password">忘记密码？</span>
              </div>
            </el-form-item>

            <el-form-item>
              <el-button
                :loading="loading"
                type="primary"
                class="login-button"
                @click.native.prevent="handleLogin"
              >
                <span v-if="!loading">立即登录</span>
                <span v-else>登录中...</span>
              </el-button>
            </el-form-item>
          </el-form>

          <div class="login-footer">
            <p class="copyright">© 2025 xx数据分析系统. All Rights Reserved.</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 响应式设计 - 移动端布局 -->
    <div class="login-mobile">
      <div class="mobile-header">
        <img src="@/assets/logo/logo-data-analysis-modern.svg" alt="系统Logo" class="mobile-logo">
        <h1>xx数据分析系统</h1>
      </div>

      <div class="mobile-form">
        <el-form 
          ref="loginForm" 
          :model="loginForm" 
          :rules="loginRules" 
          class="mobile-login-form"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              placeholder="用户名"
              prefix-icon="el-icon-user"
            />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              prefix-icon="el-icon-lock"
              @keyup.enter.native="handleLogin"
            />
          </el-form-item>

          <el-form-item prop="code" v-if="captchaEnabled">
            <el-input
              v-model="loginForm.code"
              placeholder="验证码"
              prefix-icon="el-icon-picture-outline"
              style="width: 60%"
              @keyup.enter.native="handleLogin"
            />
            <img :src="codeUrl" @click="getCode" class="mobile-captcha" alt="验证码"/>
          </el-form-item>

          <el-checkbox v-model="loginForm.rememberMe" class="mobile-remember">记住密码</el-checkbox>

          <el-button
            :loading="loading"
            type="primary"
            class="mobile-login-button"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登录</span>
            <span v-else>登录中...</span>
          </el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      title: "xx数据分析系统",
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaEnabled: true,
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
/* 基础样式和响应式设计 */
.login-container {
  min-height: 100vh;
  display: flex;
  position: relative;
  overflow: hidden;
}

/* 背景样式 */
.login-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 0;
  
  .bg-gradient {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
    opacity: 0.9;
  }
  
  .bg-pattern {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: 
      radial-gradient(circle at 25% 25%, rgba(255,255,255,0.1) 0%, transparent 50%),
      radial-gradient(circle at 75% 75%, rgba(255,255,255,0.1) 0%, transparent 50%);
    animation: float 20s ease-in-out infinite;
  }
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  50% { transform: translateY(-20px) rotate(180deg); }
}

/* 主内容区域 - 桌面端 */
.login-content {
  display: flex;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  z-index: 1;
  position: relative;
  min-height: 100vh;
}

/* 左侧面板 */
.left-panel {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  color: white;
  
  .brand-section {
    max-width: 500px;
    text-align: center;
    
    .logo-container {
      margin-bottom: 2rem;
      
      .system-logo {
        width: 120px;
        height: 120px;
        filter: brightness(1.2);
      }
    }
    
    .system-title {
      font-size: 2.5rem;
      font-weight: 700;
      margin-bottom: 0.5rem;
      background: linear-gradient(45deg, #fff, #e1e8ed);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      background-clip: text;
    }
    
    .system-subtitle {
      font-size: 1.2rem;
      opacity: 0.8;
      margin-bottom: 3rem;
      font-weight: 300;
    }
    
    .system-features {
      .feature-item {
        display: flex;
        align-items: center;
        margin-bottom: 2rem;
        text-align: left;
        
        .feature-icon {
          width: 50px;
          height: 50px;
          background: rgba(255,255,255,0.2);
          border-radius: 12px;
          display: flex;
          align-items: center;
          justify-content: center;
          margin-right: 1rem;
          backdrop-filter: blur(10px);
          
          svg {
            font-size: 24px;
            color: white;
          }
        }
        
        .feature-text {
          h3 {
            font-size: 1.1rem;
            font-weight: 600;
            margin-bottom: 0.25rem;
          }
          
          p {
            font-size: 0.9rem;
            opacity: 0.8;
            margin: 0;
          }
        }
      }
    }
  }
}

/* 右侧面板 */
.right-panel {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  
  .login-form-container {
    background: rgba(255,255,255,0.95);
    backdrop-filter: blur(20px);
    border-radius: 20px;
    padding: 3rem;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 25px 50px rgba(0,0,0,0.15);
    border: 1px solid rgba(255,255,255,0.2);
    
    .login-header {
      text-align: center;
      margin-bottom: 2rem;
      
      h2 {
        font-size: 1.8rem;
        color: #2c3e50;
        margin-bottom: 0.5rem;
        font-weight: 600;
      }
      
      p {
        color: #7f8c8d;
        margin: 0;
        font-size: 0.9rem;
      }
    }
    
    .login-form {
      .el-form-item {
        margin-bottom: 1.5rem;
        
        .el-input__inner {
          border-radius: 10px;
          border: 2px solid #e1e8ed;
          padding: 12px 15px;
          font-size: 0.9rem;
          transition: all 0.3s ease;
          
          &:focus {
            border-color: #1890ff;
            box-shadow: 0 0 0 3px rgba(24,144,255,0.1);
          }
        }
        
        .el-input__prefix {
          color: #95a5a6;
        }
      }
      
      .code-item {
        display: flex;
        align-items: center;
        gap: 1rem;
        
        .captcha-container {
          flex-shrink: 0;
          
          .captcha-img {
            height: 40px;
            border-radius: 8px;
            cursor: pointer;
            border: 2px solid #e1e8ed;
            transition: all 0.3s ease;
            
            &:hover {
              border-color: #1890ff;
              transform: scale(1.05);
            }
          }
        }
      }
      
      .login-options {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 1rem;
        
        .forgot-password {
          color: #667eea;
          font-size: 0.85rem;
          cursor: pointer;
          transition: color 0.3s ease;
          
          &:hover {
            color: #36cfc9;
          }
        }
      }
      
      .login-button {
        width: 100%;
        height: 45px;
        border-radius: 10px;
        background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
        border: none;
        font-size: 1rem;
        font-weight: 600;
        transition: all 0.3s ease;
        box-shadow: 0 4px 15px rgba(24,144,255,0.3);
        
        &:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(24,144,255,0.4);
          }
        
        &:active {
          transform: translateY(0);
        }
      }
    }
    
    .login-footer {
      text-align: center;
      margin-top: 2rem;
      
      .copyright {
        color: #95a5a6;
        font-size: 0.8rem;
        margin: 0;
      }
    }
  }
}

/* 移动端样式 */
.login-mobile {
  display: none;
  width: 100%;
  min-height: 100vh;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  padding: 1rem;
  
  .mobile-header {
    text-align: center;
    padding: 2rem 0;
    color: white;
    
    .mobile-logo {
      width: 80px;
      height: 80px;
      margin-bottom: 1rem;
    }
    
    h1 {
      font-size: 1.5rem;
      font-weight: 600;
      margin: 0;
    }
  }
  
  .mobile-form {
    .mobile-login-form {
      .el-form-item {
        margin-bottom: 1rem;
        
        .el-input__inner {
          border-radius: 10px;
          border: 2px solid rgba(255,255,255,0.3);
          background: rgba(255,255,255,0.1);
          color: white;
          backdrop-filter: blur(10px);
          
          &::placeholder {
            color: rgba(255,255,255,0.8);
          }
        }
      }
      
      .mobile-captcha {
        width: 80px;
        height: 35px;
        border-radius: 6px;
        margin-left: 0.5rem;
        cursor: pointer;
        border: 2px solid rgba(255,255,255,0.3);
      }
      
      .mobile-remember {
        margin: 1rem 0;
        color: white;
        
        .el-checkbox__label {
          color: white;
        }
      }
      
      .mobile-login-button {
        width: 100%;
        height: 45px;
        border-radius: 10px;
        background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
        border: none;
        font-size: 1rem;
        font-weight: 600;
      }
    }
  }
}

/* 响应式断点 */
@media (max-width: 768px) {
  .login-content {
    display: none;
  }
  
  .login-mobile {
    display: block;
  }
}

@media (max-width: 480px) {
  .login-mobile {
    padding: 0.5rem;
    
    .mobile-header {
      padding: 1rem 0;
      
      .mobile-logo {
        width: 60px;
        height: 60px;
      }
      
      h1 {
        font-size: 1.2rem;
      }
    }
  }
}

/* 动画效果 */
.login-form-container {
  animation: slideInRight 0.8s ease-out;
}

.left-panel {
  animation: slideInLeft 0.8s ease-out;
}

@keyframes slideInRight {
  from {
    opacity: 0;
    transform: translateX(50px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes slideInLeft {
  from {
    opacity: 0;
    transform: translateX(-50px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}
</style>
